package com.kehutong.charge.service.impl;

import com.kehutong.charge.entity.ChargeLadderConfig;
import com.kehutong.charge.entity.ChargeProjectConfig;
import com.kehutong.charge.entity.ChargeTypeConfig;
import com.kehutong.charge.entity.MeterTypeConfig;
import com.kehutong.charge.service.ChargeProjectService;
import com.kehutong.common.constant.CommonConstant;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;

import java.util.List;

/**
 * @program: kht
 * @description: ChargeProjectServiceImpl
 * @author: joneron
 * @create: 2021/6/23
 **/
public class ChargeProjectServiceImpl implements ChargeProjectService {

    private static final Logger logger = LoggerFactory.getLogger(ChargeProjectServiceImpl.class);

    @Inject
    private JdbcSession jdbcSession;





    @Override
    public Page<ChargeProjectConfig> findPage(JSONObject jsonObject) {
        FindPage<ChargeProjectConfig> find = jdbcSession.findPage(ChargeProjectConfig.class)
                .eq("deleted", false)
                .like(jsonObject,"projectFeeName")
                .eq(jsonObject,"status")
                .eq(jsonObject,"communityNo")
                .eq(jsonObject,"feeTypeNo")
                .order("sort", "desc")
                .order("createTime", "desc");

        find.page(jsonObject);
        Page<ChargeProjectConfig> chargeProjectConfigPage=find.exe();
        chargeProjectConfigPage.getList().forEach(item->{
            ChargeTypeConfig chargeTypeConfig=jdbcSession.findById(ChargeTypeConfig.class,item.getFeeTypeNo());
            MeterTypeConfig meterTypeConfig=jdbcSession.findById(MeterTypeConfig.class,chargeTypeConfig.getMeterTypeNo());
            item.setMeterTypeConfig(meterTypeConfig);
            //阶梯价
            List<ChargeLadderConfig> configList = jdbcSession.findArray(ChargeLadderConfig.class)
                    .eq("chargeProjectNo",item.getId())
                    .order("usageStart","asc").exe();
            item.setLadderList(configList);
        });
        return chargeProjectConfigPage;
    }


    @Override
    public List<ChargeProjectConfig> findList(JSONObject jsonObject) {
        return jdbcSession.findArray(ChargeProjectConfig.class)
                .eq("deleted", false)
                .exe();
    }


    @Override
    public Object save(ChargeProjectConfig entity) {

        if(CommonConstant.DEFAULT_COMMUNITY_NAME.equals(entity.getCommunityName())){
            entity.setCommunityNo(CommonConstant.DEFAULT_COMMUNITY_NO);
            entity.setSort(999);
        }

        ChargeTypeConfig position = jdbcSession.findById(ChargeTypeConfig.class,entity.getFeeTypeNo());
        position.setProjectNum(position.getProjectNum()+1);
        jdbcSession.updateById(position);

        List<ChargeLadderConfig> list = entity.getLadderList();
        jdbcSession.insert(list);

        String typeName = jdbcSession.findById(ChargeTypeConfig.class,entity.getFeeTypeNo()).getFeeTypeName();
        entity.setFeeTypeName(typeName);
        return jdbcSession.insert(entity);
    }


    @Override
    public Object update(ChargeProjectConfig chargeProjectConfig) {
        String typeName = jdbcSession.findById(ChargeTypeConfig.class,chargeProjectConfig.getFeeTypeNo()).getFeeTypeName();
        chargeProjectConfig.setFeeTypeName(typeName);


        List<ChargeLadderConfig> ladderConfigs = jdbcSession.findArray(ChargeLadderConfig.class)
                .eq("chargeProjectNo",chargeProjectConfig.getId()).exe();
        for (int i = 0; i < ladderConfigs.size(); i++) {
            //先删除阶梯再添加
            jdbcSession.deleteById(ladderConfigs.get(i));
        }

        List<ChargeLadderConfig> list = chargeProjectConfig.getLadderList();
        for (int i = 0; i < list.size(); i++) {

            jdbcSession.insert(list.get(i));
        }

        return jdbcSession.updateById(chargeProjectConfig);
    }


    @Override
    public Object deleteById(String id) {
        ChargeProjectConfig result = Pool.get(ChargeProjectConfig.class, id);
        if (result != null) {
            result.setDeleted(true);
            jdbcSession.updateById(result);
        }
        return 1;
    }


    @Override
    public Object getConfig(JSONObject jsonObject) {

        ChargeProjectConfig config = jdbcSession.findOne(ChargeProjectConfig.class)
                .eq("deleted", false)
                .eq("status",true)
                .eq("communityNo", jsonObject.getString("communityNo"))
                .eq("feeTypeNo", jsonObject.getString("feeTypeNo"))
                .exe();
        if(null == config){
            return null;
        }
        List<ChargeLadderConfig> ladderList = jdbcSession.findArray(ChargeLadderConfig.class)
                .eq("deleted", false)
                .eq("chargeProjectNo", config.getId())
                .order("usageStart", "asc")
                .exe();
        config.setLadderList(ladderList);
        return config;
    }
}
